<!-- 任务执行情况 -->
<template>
  <el-dialog title="新增文件夹" :visible.sync="editFormVisible" :close-on-click-modal="false"
             :close-on-press-escape="false" @close="$emit('update:visible',false)">
    <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
      <el-form-item label="文件夹名称" prop="tags_name">
        <el-input v-model="editForm.tags_name" auto-complete="off" placeholder="填写文件夹名称"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm" size="small">取消</el-button>
      <el-button type="primary" @click.native="editSubmit" size="small">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import api from '@/utils/api.js'

  export default {
    name: 'file',
    props: ['visible', 'tid', 'docType', 'editName'], // docType 1添加 2编辑
    watch: {
      visible () {
        this.editFormVisible = this.visible
        console.log('fileId:', this.tid)
        this.editForm.tid = this.tid
        if (this.visible && this.docType === 2) {
          console.log('this.editName:', this.editName)
          this.editForm.tags_name = this.editName.tags_name
        }
      }
    },
    data () {
      return {
        editFormVisible: false,
        editForm: {
          tags_name: '',
          tid: 0
        },
        editFormRules: {
          tags_name: [
            {required: true, message: '请填写文件夹名称', trigger: 'blur'}
          ]
        }
      }
    },
    created () {

    },
    methods: {
      editSubmit () {
        // 验证信息
        let _self = this
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            console.log('提交信息')
            _self.submitBook()
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      submitBook () {
        let url_ = api.file_add
        let data_ = this.editForm
        if (this.docType === 2) {
          url_ = api.file_edit
          data_ = {}
          data_.id = this.editName.id
          data_.name = this.editForm.tags_name
        }
        this.$docpost(url_, data_).then((response) => {
          if (response.code === 0) {
            let msg_ = '文件夹新增成功'
            if (this.docType) {
              msg_ = '文件夹编辑成功'
            }
            this.$message({
              message: msg_,
              type: 'success'
            })
            this.$emit('initFile')
            this.editFormVisible = false
          } else {
            console.log('请求失败')
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      resetForm () {
        this.editFormVisible = false
        this.editForm.tags_name = ''
      }
    }
  }
</script>
